package res

import (
	"github.com/gin-gonic/gin"
)

type response struct {
	Code int    `json:"code"`
	Data any    `json:"data"`
	Msg  string `json:"msg"`
}

var codeMap = map[int]string{
	0:  "成功",
	-1: "失败",
	-2: "参数错误",
	-3: "未登录",
	-4: "未授权",
}

func Response(c *gin.Context, code int, data any, msg string) {
	c.JSON(200, response{Code: code, Data: data, Msg: msg})
}

func Ok(c *gin.Context, data any, msg string) {
	Response(c, 0, data, msg)
}

func OkWithData(c *gin.Context, data any) {
	Ok(c, data, "成功")
}

func OkWithMsg(c *gin.Context, msg string) {
	Ok(c, gin.H{}, msg)
}

func Fail(c *gin.Context, code int, data any, msg string) {
	Response(c, code, data, msg)
}

func FailWithMsg(c *gin.Context, msg string) {
	Response(c, -1, nil, msg)
}

func FailWithCode(c *gin.Context, code int) {
	msg, ok := codeMap[code]
	if !ok {
		msg = "未知错误"
	}
	Response(c, code, nil, msg)
}
